/*
 * @Author: ReinerLau lk850593913@gmail.com
 * @Date: 2022-09-22 10:55:04
 * @LastEditors: ReinerLau lk850593913@gmail.com
 * @LastEditTime: 2023-02-27 15:15:46
 * @FilePath: \robot\src\views\patroling\composables\useTemplateAction.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import { Ref, ref } from "vue";
import usePatrolingStore from "@/store/patroling";
import {
  createMissionTemplate,
  updateMissionTemplate,
} from "@/api/robot-mission-template.js";
import { ElNotification, ElMessage, ElMessageBox } from "element-plus";

const patrolingData = usePatrolingStore();

export default () => {
  // 操作模板
  const templateActionList = [
    {
      title: "新建模板",
      color: "#FF931E",
      value: "createTemplate",
    },
    {
      title: "保存模板",
      color: "#878787",
      value: "saveTemplate",
    },
    {
      title: "搜索模板",
      color: "#1F3947",
      value: "selectTemplate",
    },
    {
      title: "取消模板",
      color: "#1F3947",
      value: "cancelTemplate",
    },
  ];

  const templateListVisible = ref(false);

  /**
   * @description: 确定模板
   * @param {*} template
   * @return {*}
   */
  const missionId = ref(0);
  function handleConfirmTemplate(template) {
    //模板id
    updateMissionId(template.id);
    patrolingData.selectTemplate(JSON.parse(template.mission));
  }

  const createTemplateVisible = ref(false);

  const formOptions = ref([
    {
      prop: "name",
      label: "任务模板名称",
    },
    {
      prop: "memo",
      label: "任务描述",
    },
  ]);

  const formData: Ref<{ name?: string; memo?: string }> = ref({});

  /**
   * @description: 确定新建模板 - 开始绘制路径
   * @param {*} data
   * @return {*}
   */
  function handleConfirm(data) {
    createTemplateVisible.value = false;
    formData.value = { ...data };
    // 已选择模板直接保存
    if (missionId.value) {
      createTemplate();
    } else {
      patrolingData.startCreatePath();
    }
  }

  /**
   * @description: 取消模板
   * @return {*}
   */
  function handleCancelTemplate() {
    patrolingData.clearPath();
  }

  /**
   * @description: 保存模板
   * @return {*}
   */
  async function handleSaveTemplate() {
    if (missionId.value) {
      ifAddExtraTemplate();
    } else {
      createTemplate();
    }
  }

  async function createTemplate() {
    if (!formData.value.name || !formData.value.memo) {
      ElNotification({
        type: "warning",
        title: "提醒",
        message: "请先新建模板",
      });
      return;
    }
    if (patrolingData.createdPath.length === 0) {
      ElNotification({
        type: "warning",
        title: "提醒",
        message: "请先创建路径",
      });
      return;
    }

    const data = {
      mission: JSON.stringify(patrolingData.createdPath),
      name: formData.value.name,
      memo: formData.value.memo,
      rtype: "patroling",
    };

    await createMissionTemplate(data);
    ElMessage.success({
      message: "发送成功",
    });
    updateFormData({});
    updateMissionId(null);
    patrolingData.clearPath();
  }

  /**
   * @description: 编辑模板
   * @return {*}
   */
  async function handleUpdateTemplate() {
    if (patrolingData.createdPath.length === 0) {
      ElNotification({
        type: "warning",
        title: "提醒",
        message: "请先创建路径",
      });
    }

    const data = {
      id: missionId.value,
      mission: JSON.stringify(patrolingData.createdPath),
      rtype: "patroling",
    };

    await updateMissionTemplate(data);
    ElMessage.success({
      message: "发送成功",
    });
    updateFormData({});
    updateMissionId(null);
    patrolingData.clearPath();
  }

  /**
   * @description: 更新模板id
   * @param {*} value
   * @return {*}
   */
  function updateMissionId(value) {
    missionId.value = value;
  }

  function updateFormData(value) {
    formData.value = value;
  }

  async function ifAddExtraTemplate() {
    try {
      await ElMessageBox.confirm("是否额外新增模板?", "提示", {
        confirmButtonText: "是",
        cancelButtonText: "否",
        type: "info",
      });
      createTemplateVisible.value = true;
    } catch {
      handleUpdateTemplate();
    }
  }

  return {
    templateActionList,
    templateListVisible,
    handleConfirmTemplate,
    createTemplateVisible,
    formOptions,
    handleConfirm,
    formData,
    handleCancelTemplate,
    handleSaveTemplate,
    missionId,
    handleUpdateTemplate,
    updateMissionId,
    updateFormData,
    ifAddExtraTemplate,
  };
};
